# -*- coding: utf-8 -*- 
import pandas as pd
import numpy as np
from pandas import DataFrame
from sklearn.metrics import roc_auc_score
from sklearn.metrics import classification_report
from sklearn.ensemble import RandomForestClassifier
from sklearn import tree
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
import sys, getopt


# 读取数据
data = pd.read_csv(  sys.argv[1] + '.csv')

# 将预测标签Outcome数据放到第一列
front = data['Outcome']
data.drop(labels='Outcome',axis = 1, inplace = True)
data.insert(0,'Outcome',front)


y = data.iloc[:,0]
x = data.iloc[:,1:]

x_train, x_test, y_train, y_test = train_test_split(x,y,test_size=30/768)

# 决策树
# max_depth=3定义树的深度, 可以用来防止过拟合
# min_weight_fraction_leaf 定义叶子节点最少要包含多少个样本(百分比表达), 防止过拟合
dtree = tree.DecisionTreeClassifier(criterion='entropy',max_depth=3,min_weight_fraction_leaf=0.01)
dtree = dtree.fit(x_train,y_train)

# print('\n\n---决策树---')
dt_roc_auc = roc_auc_score(y_test,dtree.predict(x_test))
# print('决策树 AUC = %2.2f'%dt_roc_auc)
classification_report(y_test,dtree.predict(x_test))
# print()

# 随机森林
# max_depth=None  定义树的深度，可以用来防止过拟合
# min_samples_split=10  定义至少多少个样本的情况下才继续分叉，可以用来防止过拟合
rf = RandomForestClassifier(criterion='entropy',n_estimators=1000,max_depth=None,min_samples_split=10,min_weight_fraction_leaf=0.02)
rf.fit(x_train,y_train)
# print('\n\n---随机森林---')
rf_roc_auc = roc_auc_score(y_test,rf.predict(x_test))
# print('随机森林 AUC = %2.2f'%rf_roc_auc)
# print(classification_report(y_test,rf.predict(x_test)))
classification_report(y_test,rf.predict(x_test))
y_ = np.array(y_test)
x_test.loc[0] = {sys.argv[1]:sys.argv[2]}

pred_test  = rf.predict(x_test)
# print('预测测试集结果：',pred_test)
# print('真实结果:       ',y_)
# print("用户输入预测为：",pred_test[len(pred_test)-1])
print(pred_test[len(pred_test)-1])